package components.effects
{
	import mx.effects.Fade;
	import mx.effects.Move;
	import mx.effects.Parallel;
	import mx.effects.Zoom;
	import mx.effects.easing.Cubic;
	import mx.effects.easing.Linear;
	import mx.events.EffectEvent;
	
	import spark.effects.Resize;

	public class BaseEffects
	{
		/**
		 * 播放效果
		 * @param zoomFrom
		 * @param zoomTo
		 * @param alphaFrom
		 * @param alphaTo
		 * @param p_callback
		 * 
		 */		
		public static function play(p_target:Object, p_mode:String, originX:Number, originY:Number, zoomFrom:Number, zoomTo:Number, alphaFrom:Number, 
									alphaTo:Number, p_callback:Function = null):void
		{
			var partal:Parallel = new Parallel();
			partal.target = p_target;
			partal.duration = (p_mode == "show") ? 500 : 200;
			partal.addEventListener(EffectEvent.EFFECT_END, partalEndHandler);
			
			var zoom:Zoom = new Zoom();
			zoom.originX = originX;
			zoom.originY = originY;
			zoom.zoomHeightFrom = zoomFrom;
			zoom.zoomHeightTo = zoomTo;
			zoom.zoomWidthFrom = zoomFrom;
			zoom.zoomWidthTo = zoomTo;
			zoom.easingFunction = (p_mode == "show") ? Linear.easeOut : Linear.easeIn;
			
			var fade:Fade = new Fade();
			fade.alphaFrom = alphaFrom;
			fade.alphaTo = alphaTo;
			fade.easingFunction = (p_mode == "show") ? Linear.easeOut : Linear.easeIn;
			
			partal.children = [zoom, fade];
			partal.play();
			
			function partalEndHandler(p_evt:EffectEvent):void
			{
				partal.removeEventListener(EffectEvent.EFFECT_END, partalEndHandler);
				if(p_callback != null)
					p_callback();
			}
		}
		
		/**
		 * 展开效果
		 */
		public static function expand(p_target:Object, p_fromHeight:Number, p_toHeight:Number, p_callback:Function = null):void
		{
			var resizeEffect:Resize = new Resize(p_target);
			resizeEffect.duration = 200;
			resizeEffect.heightFrom = p_fromHeight;
			resizeEffect.heightTo = p_toHeight;
			resizeEffect.addEventListener(EffectEvent.EFFECT_END, effectEndHandler);
			resizeEffect.play();
			
			function effectEndHandler(p_evt:EffectEvent):void
			{
				var target:Resize = p_evt.target as Resize;
				target.removeEventListener(EffectEvent.EFFECT_END, effectEndHandler);
				if(p_callback != null)
					p_callback();
			}
		}
		
		/**
		 * 平移效果
		 */
		public static function transform(p_targets:Array, p_xFrom:Number, p_xTo:Number):void
		{
			var move:Move = new Move();
			move.xFrom = p_xFrom;
			move.xTo = p_xTo;
			move.duration = 500;
			move.targets = p_targets;
			move.easingFunction = Cubic.easeOut;
			if(move.isPlaying)return;
			move.addEventListener(EffectEvent.EFFECT_END, effectEndHandler);
			move.play();
			
			function effectEndHandler(p_evt:EffectEvent):void
			{
				move.removeEventListener(EffectEvent.EFFECT_END, effectEndHandler);
				for(var i:int = 0;i < p_targets.length;i++){
					p_targets[i].x = p_xTo;
				}
			}
		}
	}
}